<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>基地位置</title>
		<link rel="stylesheet" type="text/css" href="css/hui.css" />
		<link rel="stylesheet" type="text/css" href="css/my.css" />
		<style type="text/css">
		body, html,#container {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
		#allmap{width:100%;height:100%;}
    .infoBoxContent{
      margin:20px;
    }
    .infoBoxContent button{
      background-color: #008CBA;
      border: none;
      color: white;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      border-radius: 16px;
      width: 120px;
    }
    .infoBoxContent h3{
      color: white;
    }
    .infoBoxContent p{
      color:white;
    }
    .infoBoxContent:before {
      content: '';
      width: 0;
      height: 0;
      border: 20px solid transparent;
      border-top-color: #333333;
      position: absolute;
      left: 50%;
      top: 100%;
      margin-left: -20px;
    }
    
    .anchorBL{display:none;}
    .hui-icons{
    	padding: 0;
    }
		</style>
	</head>
			
	<body>
		<header class="hui-header" style="height: 50px; max-height: 50px; background: #464646;">
			<div style="height: 22px;background: #464646;"></div>
			<div id="hui-back" style="line-height: 50px;"></div>
			<h1 style="line-height: 50px;">基地位置</h1>
			<div id="hui-right" style="line-height: 50px;"></div>
		</header>
		
		<div id="container"></div>
		
		
		<script type="text/javascript" src="js/func/hui.js" charset="UTF-8"></script>
		<script type="text/javascript" src="js/func/mui.min.js" charset="utf-8"></script>
		<script type="text/javascript" src="http://api.map.baidu.com/api?v=3.0&ak=ZrNnGV0usg9bmcqWaOQENB28wWHfUpjI"></script>
		<script type="text/javascript">
			
				
			var Lat = localStorage.getItem('columnLat');
			var Lng = localStorage.getItem('columnLng');
			
			var save = JSON.parse(localStorage.getItem("basePosition"));
			console.log(save);
			/*var LatAndLog = [
				{lat: 27.0297372994, lng: 112.2843568426, id: 1, name: '衡阳', address: '湖南省长沙市芙蓉区湖南农业科技大学'},//衡阳
			]//*/
			
			var LatAndLog = [];
			var map = new BMap.Map("container", {minZoom:10,maxZoom:11}); 
			var point = new BMap.Point(Lng,Lat);
			map.centerAndZoom(point, 10); 
			var geoc = new BMap.Geocoder();
			for(var i = 0,pointsLen = save.length;i <pointsLen;i++){
				LatAndLog.push({lat: save[i].lat, lng: save[i].lng, name: save[i].powername, address: save[i].AreaName})
			}
			
			
	
			
			function addMarker(points){  // 创建图标对象    
 
				// 创建标注对象并添加到地图   
					console.log(points);
				for(var i = 0,pointsLen = points.length;i <pointsLen;i++){
					var point = new BMap.Point(points[i].lng,points[i].lat);	
					var  marker = new BMap.Marker(point);   
					map.addOverlay(marker); 
					console.log(points[i]);
					//给标注点添加点击事件。使用立即执行函数和闭包
					(function() {
						var thePoint = points[i];
						marker.addEventListener("click",function(e){
							var pt = e.point;
							console.log(e);
							console.log(thePoint);
							var _that = this;
							geoc.getLocation(new BMap.Point(thePoint.lng, thePoint.lat), function(rs){
								console.log(rs);
								var addComp = rs.addressComponents;
								//alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
								showInfo(_that,thePoint,addComp);
							});
							map.panTo(new BMap.Point(thePoint.lng,thePoint.lat));
							map.setZoom(map.getZoom() + 2);;
//							showInfo(_that,thePoint);
						});
					})();
				} 
			}
			console.log(LatAndLog);
			addMarker(LatAndLog);//添加标注。
			
			//显示信息窗口，显示标注点的信息。
			function showInfo(thisMaker,currentPoint,address){
				console.log(thisMaker);
				console.log(currentPoint);
				console.log(currentPoint.lat + '----------' + currentPoint.lng);
				var sContent =
				'<ul>'
				+'<li style="line-height: 26px;font-size: 15px;">'
				+'<span style="width: 60px;display: inline-block;">基地名：</span>' + currentPoint.name + '</li>'
				+'<li style="line-height: 26px;font-size: 15px;"><span style="width: 45px;display: inline-block;">地址：</span>' + address.province + address.city + address.district + address.street + address.streetNumber + '</li>'; 
				if(plus.os.name == "Android"){
					sContent += '<li style="line-height: 26px;font-size: 15px; border: 1px solid #999; text-align: center;" onclick="checkApp('+ currentPoint.lat +',' + currentPoint.lng +')"><i class="hui-icons hui-icons-up"></i>我要导航</li>' 
				}else if ( plus.os.name == "iOS" ) {
					sContent += '<li style="line-height: 26px;font-size: 15px; border: 1px solid #999; text-align: center;" onclick="launchTest('+ currentPoint.lat +',' + currentPoint.lng +')"><i class="hui-icons hui-icons-up"></i><span>我要导航</span></li>'
				} 
				
				sContent += '</ul>';
				var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
			   	thisMaker.openInfoWindow(infoWindow);   //图片加载完毕重绘infowindow
			}
			function checkApp(lat, lng){ 
				
				if(plus.runtime.isApplicationExist({pname:'com.baidu.BaiduMap'})){
					var geolocation = new BMap.Geolocation();
				    geolocation.getCurrentPosition(function(r){
				        if(this.getStatus() == BMAP_STATUS_SUCCESS){
				            
				            window.location.href="baidumap://map/direction?region=beijing&origin=" + r.point.lat + "," + r.point.lng + "&destination=" + lat + "," + lng + "&coord_type=gcj02&mode=driving&src=andr.baidu.openAPIdemo";
				        }
				        else {
				            alert('failed'+this.getStatus());
				        }
				    },{enableHighAccuracy: true})
					//console.log("百度地图已安装"); 
//					window.location.href="bdapp://map/geocoder?location=" + lat + "," + lng + "&coord_type=gcj02&src=andr.baidu.openAPIdemo";
					//window.location.href="baidumap://map/direction?region=beijing&origin=" + lat + "," + lng + "&destination=湖南农业大学文渊馆&coord_type=bd09ll&mode=driving&src=andr.baidu.openAPIdemo";
				}else{ 
					//console.log("百度地图未安装"); 
					launchTest(lat, lng);
				}
			}
			function launchTest(lat, lng) {
				//console.log(lat + '========' +lng);
			    if ( plus.os.name == "Android" ) { 
			        plus.runtime.launchApplication( {pname:"com.baidu.BaiduMap"
						,extra:{url:"bdapp://map/geocoder?location=" + lat + "," + lng + "&src=andr.baidu.openAPIdemo"}}, function ( e ) {
							alert( "请检查当前设备是否已安装百度地图APP：" + e.message );
					} );
			    } else if ( plus.os.name == "iOS" ) {
			        plus.runtime.launchApplication( {action:"baidumap://map/geocoder?location=" + lat + "," + lng + "&coord_type=gcj02&src=ios.baidu.openAPIdemo"}, function ( e ) {
			            alert( "请检查当前设备是否已安装百度地图APP：" +  e.message );
			        } );
			    	/*plus.runtime.launchApplication( {action:"baidumap://map/direction?origin=34.264642646862,108.95108518068&destination=40.007623,116.360582&coord_type=bd09ll&mode=driving&src=ios.baidu.openAPIdemo"}, function ( e ) {
			            alert( "Open system default browser failed: " + e.message );
			        } );*/
			    }
			}
			
			

			//打开（下载）App
			function openApp(lat, lng){
				
				var ua = window.navigator.userAgent.toLowerCase();
				//微信
			    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
			    	window.location.href='downLoadForPhone';
			    }else{//非微信浏览器
			    	if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
			    	    var loadDateTime = new Date();
			    	    window.setTimeout(function() {
			    	    	var timeOutDateTime = new Date();
			    	    	if (timeOutDateTime - loadDateTime < 5000) {
			    	    		window.location = "";//ios下载地址
			    	    	} else {
			    	        window.close();
			    	    	}
			    	    },2000);
			    	    window.location = 'baidumap://map/geocoder?location=' + lat + ',' + lng + '&coord_type=gcj02&src=ios.baidu.openAPIdemo';
			    	  }else if (navigator.userAgent.match(/android/i)) {
			    	    var state = null;
			    	    try { 
			    	    	window.location = 'schema://';
			    	    	setTimeout(function(){
			    				window.location= ""; //android下载地址
			 
			    			},500);
			    	    } catch(e) {}
				    }
			    }
			}
			
			
		</script>

	</body>

</html>